#include <avr/io.h>
#include "key.h"
#include "global.h"
#include "OLED.h"
#include "ui.h"
#include "spi.h"


#define VIBRATOR_ON() 		setbit(PORT_CTRL_OUT,PIN_VIBRATOR)
#define VIBRTOR_OFF()		clrbit(PORT_CTRL_OUT,PIN_VIBRATOR)
//==========================================================================

const char msg_welcome[] = "Hell Watch\nText Reader";
const char msg_pwr_off[] = "Power Off...";

extern uint8_t g_cursor_X, g_cursor_Y;

#define OLED_BRIGHTNESS_STEP		51

void ui_init(void)
{
	OLED_print_string(msg_welcome);
	OLED_Reflash();
	get_key_value();//wait main key up
	long_delay();

	OLED_setBrightness(OLED_BRIGHTNESS_STEP*3);
}

void adjust_brightness(char dir)
{
	static uint8_t brightness = OLED_BRIGHTNESS_STEP*3;

	if(dir > 0) {
		if(brightness <= 255 - OLED_BRIGHTNESS_STEP) {
			brightness += OLED_BRIGHTNESS_STEP;
		}
	} else {
		if(brightness >= OLED_BRIGHTNESS_STEP) {
			brightness -= OLED_BRIGHTNESS_STEP;
		}
	}
	OLED_setBrightness(brightness);
}

void text_reader(void)
{
	char text_buffer[64];
	char disp_buffer[3];
	uint8_t key;
	uint32_t file_idx = 0;
	uint8_t buffer_idx = 0;
	bool file_end = false;

	do {
		if(!file_end) {
			spi_read_data((file_idx >> 16)&0xff, file_idx&0xFFFF, 
								(uint8_t *)text_buffer, sizeof(text_buffer));
			buffer_idx = 0;
			while(buffer_idx < sizeof(text_buffer)) {
				disp_buffer[0] = text_buffer[buffer_idx++];
				if(disp_buffer[0] < 0x80) {
					disp_buffer[1] = '\0';
				} else if(disp_buffer[0] != 0xff) {
					if(OLED_almost_offscreen()) {
						buffer_idx--;
						break;
					}
					disp_buffer[1] = text_buffer[buffer_idx++];
					disp_buffer[2] = '\0';
				} else {
					file_end = true;
					break;
				}
				OLED_print_string(disp_buffer);
				if(OLED_is_offscreen()) {
					break;
				}
			}
			OLED_Reflash();
		}

		do {
			key = get_key_value();
			short_delay();
		} while(key == KEY_NONE);

		switch(key) {
			case KEY_BTM_LEFT:
			case KEY_3WS_UP:
				if(file_idx >= 48) {
					spi_read_data(((file_idx-48) >> 16)&0xff, (file_idx-48)&0xFFFF, 
							(uint8_t *)text_buffer, 48);
					uint8_t count=0;
					for(uint8_t i=0; i<48; i++) {
						if(text_buffer[i] < 0x80) {
							count++;
						}

					}
					count &= 0x01;
					file_idx = file_idx - 48 + count;
				} else {
					file_idx = 0;
				}
				file_end = false;
				break;
			case KEY_BTM_RIGHT:
			case KEY_3WS_DOWN:
				if(!file_end) {
					file_idx += buffer_idx;
				}
				break;
			case KEY_3WS_PUSH:
				break;
			case KEY_TOP_RIGHT:
				adjust_brightness(1);
				break;
			case KEY_TOP_LEFT:
				adjust_brightness(0);
				break;
			case KEY_MAIN:
				return;
				break;
			default:
				break;
		}
	}while(key != KEY_MAIN);

}

 void ui_task(void)
{
	uint8_t key = get_key_value();

	switch(key) {
		case KEY_3WS_UP:
			break;
		case KEY_3WS_DOWN:
			break;
		case KEY_3WS_PUSH:
			text_reader();
			OLED_print_string(msg_welcome);
			OLED_Reflash();
			break;
		case KEY_TOP_RIGHT:
			adjust_brightness(1);
			break;
		case KEY_TOP_LEFT:
			adjust_brightness(0);
			break;
		case KEY_MAIN://PWR Off
			power_off();
			break;
		default:
			break;
	}
}
	
void power_off(void)
{
	OLED_print_string(msg_pwr_off);
	OLED_Reflash();
	long_delay();
	OLED_OFF();
	long_delay();
	vbat_m_pwr_free();
	OLED_TO_RST();
	long_delay();
	pwr_lock_free();//LDO off
} 

